package Controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;

import View.SettingDistributionsView;
import 	Communication.Client;
import Communication.MessageHandle;
import DB.CategoryProductQuery;
import DB.CategoryQuery;
import DB.ProductQuery;
import Entity.Category;
import Entity.Customer;
import Entity.Product;

/** This class controls and manages the process that allows to create distribution for categories */
public class SettingDistributionsController implements Serializable {
	static final long serialVersionUID = 42L; 
	/**  Required attributes */
	static SettingDistributionsView SettingDistributionsView;
	Client client;
	CategoryQuery categoryModel;
	static Category ctg;
	static ArrayList<Category> categoryList=new ArrayList<Category>();
	CategoryProductQuery categoryProductModel;
	ProductQuery productModel;

	/**
	 * Constructor calling to listener class that attached to the process
	 * @param client
	 * @param workerMainMenuController
	 */
	public SettingDistributionsController(Client client, WorkerMainMenuController workerMainMenuController) {
		this.client = client;
		SettingDistributionsView =  new SettingDistributionsView();
		SettingDistributionsView.showWindow();
		SettingDistributionsView.addSettingDistributionsListener(new SettingDistributionsListener(workerMainMenuController));

	}

	/**  Inner Listener class */ 
	class SettingDistributionsListener implements ActionListener 
	{
		WorkerMainMenuController workerMainMenuController;

		/**
		 * creats SettingDistributions Listener
		 * @param workerMainMenuController
		 */
		public SettingDistributionsListener(WorkerMainMenuController workerMainMenuController) {
			this.workerMainMenuController = workerMainMenuController;

			categoryModel = new CategoryQuery();
			categoryProductModel = new CategoryProductQuery();
			productModel = new ProductQuery(client);


		}



		@Override
		/**
		 * Handle Actions
		 */
		public void actionPerformed(ActionEvent event) {

			if (event.getSource() == SettingDistributionsView.btnBack()) {
				SettingDistributionsView.hideWindow();
				workerMainMenuController.showWindow();
				SettingDistributionsView.clearFields();	
			}
			else if (event.getSource() == SettingDistributionsView.btnSet()) {


				if ((categoryModel.isEmpty(SettingDistributionsView.gettxtCatID())) ||
						(categoryModel.isEmpty(SettingDistributionsView.gettxtCategory())) || 
						(categoryModel.isEmpty(SettingDistributionsView.getProduct())) )
					SystemController.displayErrorMessage("One or more fields are Empty");

				else{

					SettingDistributionsView.showWindow();

					ctg=new Category();
					ctg.setTypeID(SettingDistributionsView.gettxtCatID());
					ctg.setTypeName(SettingDistributionsView.gettxtCategory());
					ctg.setCategoryName(SettingDistributionsView.gettxtName());
					ctg.setProductID(SettingDistributionsView.getProduct());

					try {
						MessageHandle msg = new MessageHandle(ctg, MessageHandle.CATEGORY, false);
						client.sendToServer(msg);
					} catch (Exception e) {

						// TODO Auto-generated catch block
						e.printStackTrace( );
					}
					workerMainMenuController.hideWindow();
				}
			}
		}

	}


	/**
	 * handles the message received from server - if the DB was updated or not and why.
	 * @param msg
	 */
	public static void updataView(String msg) {
		// TODO Auto-generated method stub
		if (msg.compareTo("new")==0){
			SystemController.displayMessage("New Distribution successfully added");
			SettingDistributionsView.clearFields();	
		}
		else if( msg.compareTo("product not exists")==0 ) 
			SystemController.displayErrorMessage("Setting Distributions Failed: This Product Does Not Exist");
		else if( msg.compareTo("category name  exist")==0 ) 
			SystemController.displayErrorMessage("Setting Distributions Failed: This Category Name and Type Already Exist");
		else if( msg.compareTo("category name and type  exist")==0 ) 
			SystemController.displayErrorMessage("Setting Distributions Failed: This Category Name Already Exist");
		else if(msg.compareTo("category id exist")==0)
			SystemController.displayErrorMessage("Setting Distributions Failed: This Category ID Already Exist");

	}

}


